import datetime

import openpyxl
import pandas as pd

import TimeUtil
import 排序分公司
from 单用户中断时长工作.Input import get_four_path


def w1(d):
    start_row = 3
    start_row_2 = 3

    df = 排序分公司.order(pd.read_excel(get_four_path(date)))

    workbook = openpyxl.load_workbook(f"D:\\中断输入\\输出\\单用户中断.xlsx")
    sheet = workbook['单用户中断时长']
    # 修改新插入行的单元格内容
    for i in range(start_row, start_row + 16):
        sheet.cell(row=i, column=d * 3 + 5).value = df['中断总时长分'].loc[i - start_row]
        sheet.cell(row=i, column=d * 3 + 6).value = df['活跃用户数'].loc[i - start_row]
        sheet.cell(row=i, column=d * 3 + 7).value = df['单用户中断时长'].loc[i - start_row]

    sheet = workbook['中断用户平均修复时长']
    # 修改新插入行的单元格内容
    for i in range(start_row, start_row + 16):
        sheet.cell(row=i, column=d * 3 + 5).value = df['中断总时长分'].loc[i - start_row]
        sheet.cell(row=i, column=d * 3 + 6).value = df['中断用户数'].loc[i - start_row]
        sheet.cell(row=i, column=d * 3 + 7).value = df['中断用户平均修复时长'].loc[i - start_row]

    sheet = workbook['用户平均中断率']
    # 修改新插入行的单元格内容
    for i in range(start_row_2, start_row_2 + 16):
        sheet.cell(row=i, column=d * 3 + 5).value = df['中断用户数'].loc[i - start_row]
        sheet.cell(row=i, column=d * 3 + 6).value = df['活跃用户数'].loc[i - start_row]
        sheet.cell(row=i, column=d * 3 + 7).value = df['用户平均中断率'].loc[i - start_row]

    workbook.save(f"D:\\中断输入\\输出\\单用户中断.xlsx")


"""
每月初需要修改模版，以及注意前一天的文件名
月初
1.新建"D:\\中断输入\\输出\\2.0日单用户中断时长各分公司指标.xlsx"
2.月指标新增2.0sheet
"""
if __name__ == '__main__':
    # 起始日期
    start_date = datetime.strptime('2025-03-26', '%Y-%m-%d')
    # 结束日期
    end_date = datetime.strptime('2025-04-05', '%Y-%m-%d')

    # 遍历日期
    date = start_date
    while date <= end_date:
        time = date.strftime('%Y-%m-%d')
        print(time)
        # 日期加 1 天
        date += datetime.timedelta(days=1)

        delta = date - TimeUtil.parse('2025-03-26')
        # 获取天数差值
        day = delta.days
        print(day)
        w1(day)
